<template>
  <div class="proDialog">
    <el-dialog title="选择优惠券"
      :visible.sync="visible"
      width="1000px"
      z-index="2000"
      append-to-body
      @close="toCancelBox">
      <el-form class="formList"
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="100px"
        style="padding-top: 0">

        <el-form-item label="优惠券名称"
          prop="couponName">
          <el-input v-model="queryParams.couponName"
            placeholder="请输入优惠券名称"
            clearable
            size="small" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery">搜索</el-button>
        </el-form-item>
      </el-form>

      <el-table border
        ref="indexTable"
        :data="infoList"
        tooltip-effect="dark"
        style="width: 100%"
        @row-click="singleElection">

        <el-table-column label=""
          width="40">
          <template slot-scope="scope">
            <el-radio class="radio"
              v-model="templateSelection"
              :label="scope.$index"></el-radio>
          </template>
        </el-table-column>

        <el-table-column label="图标"
          align="center"
          prop="couponIconUrl">
          <template slot-scope="scope">
            <el-image v-if="scope.row.couponIconUrl != null && scope.row.couponIconUrl != '' "
              slot="reference"
              :src="scope.row.couponIconUrl"
              class="image-rows"
              :preview-src-list="[scope.row.couponIconUrl]">
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="优惠券名称"
          align="center"
          prop="couponName"
          :show-overflow-tooltip="true"
          min-width="120">

        </el-table-column>
        <el-table-column label="类型"
          align="center"
          prop="couponType" />
        <el-table-column label="优惠内容"
          align="center"
          prop="couponAmount"
          :show-overflow-tooltip="true"
          width="150">
          <template slot-scope="scope">
            <span>{{ scope.row.amountStr }}</span>
          </template>
        </el-table-column>

        <el-table-column label="开始时间"
          align="center"
          prop="startDate"
          width="160"
          sortable>
          <template slot-scope="scope">
            <span>{{
            parseTime(scope.row.startDate, "{y}-{m}-{d} {h}:{i}:{s}")
            }}</span>
          </template>
        </el-table-column>
        <el-table-column label="结束时间"
          align="center"
          prop="endDate"
          width="160"
          sortable>
          <template slot-scope="scope">
            <span>{{
            parseTime(scope.row.endDate, "{y}-{m}-{d} {h}:{i}:{s}")
            }}</span>
          </template>
        </el-table-column>
        <el-table-column label="有效时长"
          align="center"
          prop="expireDays"
          :show-overflow-tooltip="true" />

      </el-table>

      <pagination v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
        :page-sizes="pageSizess" />

      <div style="display:flex; justify-content: flex-end;">
        <el-button @click="determine"
          type="primary">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>

import {
  listCoupon,
  getCoupon,
  delCoupon,
  addCoupon,
  updateCoupon,
  updateStatus,
  getCouponRule,
  addCouponRule,
  updateCouponRule
} from "@/api/activity/coupon";
export default {
  props: {
    flag: {
      type: Boolean,
    },

  },
  watch: {
    flag (val) {
      this.visible = this.flag;
    },
  },
  data () {
    return {
      templateSelection: '',
      // 显示搜索条件
      showSearch: true,
      datas: null,
      selectedDatas: [],
      pageSizess: [5, 10, 15, 20, 25, 30, 50],
      // 商品信息表格数据
      infoList: [],
      // indexTable: [],
      infoListHistory: [],
      multipleSelection: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        storeName: null,
        dataStatus: '1',
      },
      visible: false,
      categoryOptions: [],
      multipleSelectionData: {},
    };
  },
  created () {
    this.visible = this.flag;
    this.getList();

  },
  methods: {
    singleElection (row) {
      this.multipleSelectionData = row;
      this.templateSelection = this.infoList.indexOf(row);
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    toCancelBox () {
      this.visible = false;
      this.$emit("toCloseBox", false);
    },
    //回显选中
    greet (selectedData) {
      if (Object.keys(selectedData).length > 0) {
        this.multipleSelectionData = selectedData;
      } else {
        this.multipleSelectionData = {};
      }
      this.singleElection(this.multipleSelectionData)
    },

    /** 确定选择商品 */
    determine () {
      this.$emit("multipleSelection", this.multipleSelectionData);

      this.$forceUpdate();
      this.open = false;
    },
    /** 查询商品信息列表 */
    getList () {
      listCoupon(this.queryParams).then((response) => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
        if (Object.keys(this.multipleSelectionData).length > 0) {
          setTimeout(() => {
            let index = -1;
            this.infoList.forEach((vo, ind) => {
              if (this.multipleSelectionData.couponUid == vo.couponUid) {
                index = ind
              }
            })
            this.templateSelection = index;
          });

        }


      });
    },
  },
};
</script>
<style scoped>
.determine {
  position: relative;
  color: #fff;
}

.pagination-container {
  position: relative;
}

.image-rows {
  width: 80px;
  height: 80px;
}

/deep/ .el-radio__label {
  display: none;
}
</style>
